<%@page import="java.sql.ResultSet"%>
<%
		String UserName = request.getParameter("UserName");
		String Password1 = request.getParameter("Password1");
		String FirstName = request.getParameter("FirstName");
		String LastName = request.getParameter("LastName");
		String Address = request.getParameter("Address");
		String City = request.getParameter("City");
		String State = request.getParameter("State");
		String ZipCode = request.getParameter("ZipCode");
		String Phone = request.getParameter("Phone");
		
		String Email = request.getParameter("Email");
		String CreditCard = request.getParameter("CreditCard");
		
		String SSN = request.getParameter("SSN");
		String StartDate = request.getParameter("StartDate");
		String HourlyRate = request.getParameter("HourlyRate");
		String Position = request.getParameter("Position");
				
		String mysJDBCDriver = (String)session.getValue("mysJDBCDriver"); 
		String mysURL = (String)session.getValue("mysURL"); 
	   	String mysUserID = (String)session.getValue("mysUserID");
	  	String mysPassword = (String)session.getValue("mysPassword");
/*    	
	if ((username!=null) &&(userpasswd!=null))
	{
		if (username.trim().equals("") || userpasswd.trim().equals(""))
		{
			response.sendRedirect("index.htm");
		}
		else
		{
*/
			// code start here
			java.sql.Connection conn=null;
			try 
			{
            	Class.forName(mysJDBCDriver).newInstance();
    			java.util.Properties sysprops=System.getProperties();
    			sysprops.put("user",mysUserID);
    			sysprops.put("password",mysPassword);
        
				//connect to the database
    			conn=java.sql.DriverManager.getConnection(mysURL,sysprops);
    			System.out.println("Connected successfully to database using JConnect");
    
    			java.sql.Statement stmt1=conn.createStatement();
    			
    			//to increment PersonId
    			ResultSet rs = stmt1.executeQuery("SELECT PersonId FROM Person");
    			int PersonId=0;
    			if(rs.last()){
    			PersonId=rs.getInt("PersonId")+1;
    			}

            		
           if (request.getParameter("target").trim().equals("customer"))
           {
							stmt1.executeUpdate("insert into Person(PersonId,LastName,FirstName,Address,City,State,ZipCode,Phone) VALUES('"+PersonId+"','"+LastName+"','"+FirstName+"','"+Address+"','"+City+"','"+State+"','"+ZipCode+"','"+Phone+"')");
//							out.print("insert into Person(PersonId,LastName,FirstName,Address,City,State,ZipCode,Phone) VALUES('"+PersonId+"','"+LastName+"','"+FirstName+"','"+Address+"','"+City+"','"+State+"','"+ZipCode+"','"+Phone+"')");
							
							stmt1.executeUpdate("INSERT into Customer(PersonId,CreditCard,Email,UserName,Rating) VALUES('"+PersonId+"','"+CreditCard+"','"+Email+"','"+UserName+"',1)");

							stmt1.close();
						}
					else
           {
						stmt1.executeUpdate("insert into Person(PersonId,LastName,FirstName,Address,City,State,ZipCode,Phone) VALUES('"+PersonId+"','"+LastName+"','"+FirstName+"','"+Address+"','"+City+"','"+State+"','"+ZipCode+"','"+Phone+"')");
//						out.print("insert into Person(PersonId,LastName,FirstName,Address,City,State,ZipCode,Phone) VALUES('"+PersonId+"','"+LastName+"','"+FirstName+"','"+Address+"','"+City+"','"+State+"','"+ZipCode+"','"+Phone+"')");

						stmt1.executeUpdate("insert into Employee(PersonId,SSN,StartDate,HourlyRate,Position) VALUES('"+PersonId+"','"+SSN+"','"+StartDate+"','"+HourlyRate+"','"+Position+"')");
							stmt1.close();
						}
			} catch(Exception e)
			{
				e.printStackTrace();
				out.print(e.toString());
			}
			finally{
			
				try{conn.close();}catch(Exception ee){};
			}
/*
	}
*/
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<Title>User Added!</Title>
</head>
<body>
<div class="main">
  <div class="header">
    <div class="resize">
      <div class="logo"><img src="images/logo.png" alt="picture" width="171" height="42" border="0" /></a></div>
      <div class="clr"></div>
    </div>
    <div class="clr"></div>
  </div>
  <div class="slider_resize">
    <div class="clr"></div>
  </div>
  <div class="FBG">
    <div class="clr"></div>
  </div>
  <div class="body">
    <div class="resize">
      <div class="left">




<body style="text-align: center" bgcolor="#FFFFFF">
<br />
<TABLE style="background-color: #FFFFFF;" WIDTH="40%" border="1">

<tr><th>User successfully inserted into the database! Please click on the Home button below to login with your new account.</th></tr>

</table>
<a href="index.htm">Home</a>
<br />
<br />
</body>
</div>
<div class="clr"></div>
</div>
  </div>
  <div class="footer">
    <div class="resize" style="background:#141414; font-color:#ffffff">
      <p class="leftt">Created by: Andrew Gratta, Sarah Jordan, and Kevin O'Flaherty<br />
      <p class="rightt"><span class="leftt">Copyright. All Rights Reserved</span></p>
      <p class="rightt">Developed for CSE305 at Stony Brook University</p>
      <div class="clr"></div>
    </div>
  </div>
</div>
</body>
</html>
